#include "Stack.h"

void StackInit(Stack* pstack) {
	assert(pstack);   pstack->_top = -1;   pstack->_capacity = 4;
	pstack->_array = (StackDataType*)malloc(sizeof(StackDataType) * 4);
}

void StackDestory(Stack* pstack) {
	assert(pstack);   free(pstack->_array);   pstack->_array = NULL;
	pstack->_top = -1;   pstack->_capacity = 0;
}

void StackPrint(Stack* pstack) {
	assert(pstack);   printf("(栈顶)  ");
	for (int i = pstack->_top; i >= 0; i--) {
		printf("%d  ", pstack->_array[i]);
	}   printf("(栈底)\n");
}

void StackPush(Stack* pstack, StackDataType value) {
	assert(pstack);
	if (pstack->_top + 1 == pstack->_capacity) {
		pstack->_capacity *= 2;
		pstack->_array = (StackDataType*)realloc(pstack->_array, sizeof(StackDataType) * pstack->_capacity);
	}   pstack->_array[++(pstack->_top)] = value;
}

void StackPop(Stack* pstack) {
	assert(pstack);   if (pstack->_top >= 0) { (pstack->_top)--; }
}

int StackSize(Stack* pstack) {
	assert(pstack);   return pstack->_top + 1;
}

bool StackEmpty(Stack* pstack) {
	assert(pstack);   return pstack->_top >= 0 ? 0 : 1;
}

StackDataType StackTop(Stack* pstack) {
	assert(pstack);   assert(!StackEmpty(pstack));
	return pstack->_array[pstack->_top];
}